package com.chtw.oasystem.controller;


import com.chtw.oasystem.bean.Records;
import com.chtw.oasystem.bean.RecordsWork;
import com.chtw.oasystem.common.response.RespUtils;
import com.chtw.oasystem.respBean.ApproveFileRespBean;
import com.chtw.oasystem.respBean.FileRespBean;
import com.chtw.oasystem.respBean.RecordsResBean;
import com.chtw.oasystem.service.impl.FileUploadServiceImpl;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/file")
@ResponseBody
public class FileController {

    @Autowired
    private FileUploadServiceImpl fileUploadService;
    private static final Logger logger = LoggerFactory.getLogger(FileController.class);
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public RespUtils<FileRespBean> fileUpLoad(HttpSession session, @RequestParam("filename") MultipartFile file) {
        if (file.isEmpty()) {
            return RespUtils.fail("请选择文件！");
        } else {
            try {
                FileRespBean fileRespBean = fileUploadService.saveFile(session, file);
                if (fileRespBean != null) {
                    return RespUtils.success(fileRespBean);
                }
                return RespUtils.fail("上传文件失败！");
            } catch (Exception e) {
                logger.error(e.getMessage());
                return RespUtils.fail(e.getMessage());
            }
        }
    }

    @RequestMapping(value = "/work_upload", method = RequestMethod.POST)
    public RespUtils<FileRespBean> workFileUpLoad(HttpSession session, @RequestParam("filename") MultipartFile file) {
        if (file.isEmpty()) {
            return RespUtils.fail("请选择文件！");
        } else {
            try {
                FileRespBean fileRespBean = fileUploadService.workFileUpLoad(session, file);
                if (fileRespBean != null) {
                    return RespUtils.success(fileRespBean);
                }
                return RespUtils.fail("上传文件失败！");
            } catch (Exception e) {
                logger.error(e.getMessage());
                return RespUtils.fail(e.getMessage());
            }
        }
    }

    @GetMapping("/list")
    public RespUtils<RecordsResBean> list(HttpSession session, @RequestParam(defaultValue = "1", required = false) int page, @RequestParam(defaultValue = "10", required = false) int limit, String search){
        try{
            PageInfo<Records> info = fileUploadService.list(session, page, limit, search);
            return RespUtils.success(new RecordsResBean((int) info.getTotal(), info.getPages(), info.getList(), null));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }
    @GetMapping("/work_list")
    public RespUtils<RecordsResBean> work_list(HttpSession session, @RequestParam(defaultValue = "1", required = false) int page, @RequestParam(defaultValue = "10", required = false) int limit, String search){
        try{
            PageInfo<RecordsWork> info = fileUploadService.work_list(session, page, limit, search);
            return RespUtils.success(new RecordsResBean((int) info.getTotal(), info.getPages(), null, info.getList()));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }
    @DeleteMapping("/delete")
    public RespUtils<Boolean> delete(HttpSession session, int id){
        try{
            return RespUtils.success(fileUploadService.delete(session, id));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/detail")
    public RespUtils<ApproveFileRespBean> detail(HttpSession session, int id){
        try{
            return RespUtils.success(fileUploadService.approveFile(session, id));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }
}
